# coding: utf-8

import time
import json
import datetime

from common.consumer import ConsumerBase
from common.log import Log
from common.wechat_util import send_template_message, grequest_send


class WechatConsumer(ConsumerBase):
    """
    created by chenhj at 2017/7/25

    """
    @staticmethod
    def msg_callback(channel, method, properties, body):
        """
        created by chenhj at 2017/7/25
        发送模板消息
        """
        Log.info('{}: send wx template messages...'.format(datetime.datetime.now().strftime('%F %T')))
        body = json.loads(body)
        req_set = set()
        start = time.time()
        for each in body:
            req_set.add(send_template_message(**each))
        if req_set:
            grequest_send(req_set)
        Log.info('send wx_message used {}'.format(time.time() - start))
